package com.decent.ejfadmin.collectcard.dao;

import com.decent.ejfadmin.collectcard.dao.provider.CollectCardRecordDaoProvider;
import com.decent.ejfadmin.collectcard.entity.SupplyCardViewBean;
import com.decent.ejfadmin.collectcard.response.CollectCardRecordByBatchDto;
import com.decent.ejfadmin.collectcard.response.CollectCardRecordDto;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.SupplyCard;
import com.decent.ejfadmin.common.bean.SupplyProduct;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 收卡记录列表
 *
 * @author weicx
 * @date 2018/07/20
 */
@Mapper
@Repository
public interface CollectCardRecordDao {
    /**
     * 收卡记录列表查询计数
     *
     * @param userIds   会员id
     * @param startTime 查询开始时间
     * @param endTime   查询结束时间
     * @param card      销卡bean
     * @return 总数
     */
    @SelectProvider(type = CollectCardRecordDaoProvider.class, method = "apiQuerySupplyOrderListCount")
    int apiQuerySupplyOrderListCount(@Param("userIds") String[] userIds, @Param("startTime") String startTime,
                                     @Param("endTime") String endTime, @Param("card") SupplyCardViewBean card);

    /**
     * 分页查询收卡列表(批次分组)计数
     *
     * @param userIds   会员id
     * @param startTime 查询开始时间
     * @param endTime   查询结束时间
     * @param card      销卡bean
     * @param pageUnit  分页组件
     * @return list
     */
    @SelectProvider(type = CollectCardRecordDaoProvider.class, method = "apiQuerySupplyOrderListPage")
    List<CollectCardRecordDto> apiQuerySupplyOrderListPage(@Param("page") Page pageUnit,
                                                           @Param("userIds") String[] userIds,
                                                           @Param("startTime") String startTime,
                                                           @Param("endTime") String endTime,
                                                           @Param("card") SupplyCardViewBean card);

    /**
     * 修改收卡状态为失败
     *
     * @param batchIds 批次号
     * @param status   状态
     * @return 修改的行数
     */
    @UpdateProvider(type = CollectCardRecordDaoProvider.class, method = "putCardFail")
    int putCardFail(@Param("batchIds") String batchIds, @Param("targetStatus") Integer status);

    /**
     * 查询指定状态、批次号的卡信息
     *
     * @param batchIds 批次号
     * @param status   状态
     * @return
     */
    @Select("select orderId,userId from yuhuang_supply_card t where  t.`status` = #{targetStatus} and t.batchId in (${batchIds})")
    List<SupplyCard> queryTargetCard(@Param("batchIds") String batchIds, @Param("targetStatus") Integer status);

    /**
     * 按批次查询收卡列表计数
     *
     * @param queryCard 查询bean
     * @return 总数
     */
    @SelectProvider(type = CollectCardRecordDaoProvider.class, method = "querySupplyOrderListByBatchCount")
    int querySupplyOrderListByBatchCount(@Param("card") SupplyCard queryCard);

    /**
     * 按批次查询收卡列表
     *
     * @param pageUnit  分页组件
     * @param queryCard 查询bean
     * @return list
     */
    @SelectProvider(type = CollectCardRecordDaoProvider.class, method = "querySupplyOrderListByBatchPage")
    List<CollectCardRecordByBatchDto> querySupplyOrderListByBatchPage(@Param("page") Page pageUnit, @Param("card") SupplyCard queryCard);

    /**
     * 根据id 查询产品信息
     *
     * @param id
     * @return
     */
    @Select(" select k.product_name,p.card_operator_name from yuhuang_card_operator p,yuhuang_supply_product k where p.card_operator_id = k.product_type and k.card_product_id = #{id} ")
    SupplyProduct queryProductInfoById(@Param("id") Long id);

    /**
     * 根据 面值+运营商+地区 查询产品信息
     *
     * @param area
     * @param face
     * @param productType
     * @return
     */
    @Select(" SELECT " +
            " k.card_product_id, " +
            " k.product_name, " +
            " p.card_operator_name  " +
            "FROM " +
            " yuhuang_card_operator p, " +
            " yuhuang_supply_product k  " +
            "WHERE " +
            " p.card_operator_id = k.product_type  " +
            " AND k.face =  #{face}" +
            " AND k.product_type =  #{productType}" +
            " AND k.area = #{area}")
    SupplyProduct queryProductInfoByFaceAreaType(@Param("face") Double face, @Param("area") String area, @Param("productType") Integer productType);

    /**
     * 根据 面值+运营商+地区 查询产品信息
     *
     * @param area
     * @param face
     * @param productType
     * @return
     */
    @Select(" SELECT " +
            " k.card_product_id, " +
            " k.product_name, " +
            " p.card_operator_name  " +
            "FROM " +
            " yuhuang_card_operator p, " +
            " yuhuang_supply_product k  " +
            "WHERE " +
            " p.card_operator_id = k.product_type  " +
            " AND k.face =  #{face}" +
            " AND k.product_type =  #{productType}" +
            " AND k.area = #{area} limit 1")
    SupplyProduct querySingleProductInfoByFaceAreaType(@Param("face") Double face, @Param("area") String area, @Param("productType") Integer productType);
}
